<?php  if (!defined('BASEPATH')) exit('No direct script access allowed');

class CI_Action_log{
	var $current_dbprefix = "";
	var $CI;
	var $id = 0;
	var $action ="";
	var $user_id = 0;
	var $slug = "";
	var $parameter = "";
	var $datetime = "";
	
	function __construct() {
		$this->CI= & get_instance();
		$this->current_dbprefix = $this->CI->db->dbprefix;
		$this->CI->db->set_dbprefix('default_');
	}
	function __destruct() {
		$this->CI->db->set_dbprefix($this->current_dbprefix);
	}
    
	function init($cmd, $input)
	{
		$this->CI->db->set_dbprefix('default_');
		if($cmd == 'id')
		{
			$result = $this->CI->db->where('id',$input)->get(TBL_LOG_ACTIONS)->result();
		}
		else
		{
			return false;
		}
		
		if( !empty($result) )
		{
			$this->id = $result[0]->id;
			$this->user_id = $result[0]->user_id;
			$this->slug = $result[0]->slug;
			$this->datetime = $result[0]->datetime;
			
			return $this;
		}
		return false;
	}
	
	function setAction($input){ $this->action=$input; }
	function setUserId($input){ $this->user_id=$input; }
	function setSlug($input){ $this->slug=$input; }
	function setParameter($input){ $this->parameter=$input; }
	function setDateTime($input){ $this->datetime=$input; }
	
	function getId(){ return $this->id; }
	function getAction(){ return $this->action; }
	function getUserId(){return $this->user_id; }
	function getSlug(){return $this->slug; }
	function getParameter(){return $this->parameter; }
	function getDateTime(){return $this->datetime; }
	
	
	function update()
	{
		$this->CI->db->set_dbprefix('default_');
		$this->CI->db->set('action', $this->action);
		$this->CI->db->set('user_id', $this->user_id);
		$this->CI->db->set('slug', $this->slug);
		$this->CI->db->set('parameter', $this->parameter);
		$this->CI->db->set('datetime', $this->datetime);
		
		$this->CI->db->where('id', $this->id)->update(TBL_LOG_ACTIONS);
	}
	
	function createNewEntity(){
		$this->CI->db->set_dbprefix('default_');
		$this->CI->db->set('datetime', time());
		$this->CI->db->insert(TBL_LOG_ACTIONS);
		
		return $this->CI->db->insert_id();
	}
	
	function insertNewAction($user_id, $slug){
		$this->CI->db->set_dbprefix('default_');
		$newid = $this->createNewEntity();
		$OBJ = clone_($this->init('id',$newid));
		$OBJ->setUserId($user_id);
		$OBJ->setSlug($slug);
		$OBJ->update();
	}
	
	public function getLogActionList($action = '', $limit = array()){
		$this->CI->db->set_dbprefix('default_');
		if($action){
			$this->CI->db->where('action', $this->action);
		}
		if($limit && count($limit) > 0){
			$this->CI->db->limit($limit[0], $limit[1]);
		}
		$this->CI->db->order_by('datetime', 'DESC');
		return $this->CI->db->get(TBL_LOG_ACTIONS)->result();
	}
	
	public function countLogActionList($action = '', $limit = array()){
		$this->CI->db->set_dbprefix('default_');
		if($action){
			$this->CI->db->where('action', $this->action);
		}
		if($limit && count($limit) > 0){
			$this->CI->db->limit($limit[0], $limit[1]);
		}
		$this->CI->db->order_by('datetime', 'DESC');
		return $this->CI->db->count_all_results(TBL_LOG_ACTIONS);
	}
	public function delete($id){
		$ci=& get_instance();
		$ci->db->where('id',$id);
		$ci->db->delete(TBL_LOG_ACTIONS);
	}
	
	function do_log($action, $userId, $username, $slug, $parameter){
		$this->CI->db->set_dbprefix('default_');
		$this->CI->db->set('action', $action);
		$this->CI->db->set('user_id', $userId);
		$this->CI->db->set('username', $username);
		$this->CI->db->set('slug', $slug);
		$this->CI->db->set('parameter', implode(',', $parameter));
		$this->CI->db->set('datetime', time());
		$this->CI->db->insert(TBL_LOG_ACTIONS);
		
		return $this->CI->db->insert_id();
	}
	//end class
}
